package idv.loveloser.servlet;

import idv.loveloser.dao.MdlContext;
import idv.loveloser.dao.MdlContextDAO;
import idv.loveloser.dao.MdlCourseModules;
import idv.loveloser.dao.MdlCourseModulesDAO;
import idv.loveloser.dao.MdlFiles;
import idv.loveloser.dao.MdlFilesDAO;
import idv.loveloser.dao.MdlFolder;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddCourseFolder extends HttpServlet {
	private static final int FOLDER=6;
	private static final int CONTEXT_MODULE=70;
	private static final int CONTEXT_COURSE=50;
	private static final int PLUG_IN_ID=163995112;
	private static final String COMPONENT="user";
	private static final String FILE_AREA="draft";
	
	private MdlFilesDAO filesDAO = new MdlFilesDAO();
	private MdlCourseModulesDAO courseModuleDAO = new MdlCourseModulesDAO();
	private MdlContextDAO contextDAO = new MdlContextDAO();
	/**
	 * Constructor of the object.
	 */
	public AddCourseFolder() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String contextPath="";
		long courseModule=-1L;
		long moduleInstance=-1L;
		if(Util.isAuthorised(client, sessionKey)){
			String courseId = request.getParameter("courseId");
			List<MdlContext> contexts=contextDAO.findByContextlevel(CONTEXT_COURSE);
			for(int index=0;index<contexts.size();index++){
				MdlContext context = contexts.get(index);
				if(context.getInstanceid()==Long.parseLong(courseId)){
					contextPath=context.getPath();
					break;
				}
			}
			contexts=null;
			contexts=contextDAO.findAll();
			for(int index=0;index<contexts.size();index++){
				MdlContext context = contexts.get(index);
				if(context.getPath().startsWith(contextPath)){
					
				}
			}
			
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
